package com.rrjjg.app.core.config;

import com.rrjjg.app.core.builders.ThreadPoolBuilder;
import com.rrjjg.app.core.properties.ThreadPoolPro;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2022/8/16 12:42 <br>
 */
@Slf4j
public class MediaAsyncConfig extends AsyncConfigurerSupport {

    ThreadPoolPro poolPro;

    public MediaAsyncConfig(ThreadPoolPro poolPro) {
        this.poolPro = poolPro;
    }

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolExecutor pool = new ThreadPoolBuilder()
                .setCorePoolSize(poolPro.getCorePoolSize())
                .setMaximumPoolSize(poolPro.getMaximumPoolSize())
                .setKeepAliveTime(poolPro.getKeepAliveTime())
                .setQueueCapacity(poolPro.getQueueCapacity())
                .setThreadFactory(new ThreadPoolBuilder.AsyncThreadFactory("async-")).build();
        return pool;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (ex, method, params) -> log.error("async-pool method ={}, params={}, error", method, params, ex);
    }
}
